﻿using Microsoft.Reporting.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZXing.QrCode;
using ZXing;
using static System.Net.Mime.MediaTypeNames;
using System.Runtime.CompilerServices;
using ZXing.Common;

namespace WindowsFormsApp1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            button1_Click(null,null);
        }

        private void Form2_Load(object sender, EventArgs e)
        {

           // this.reportViewer1.RefreshReport();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //DataTable dt = new DataTable();//可以查询数据库填充datatable
         
            var tab = DbContext.Db.Ado.UseStoredProcedure().GetDataTable("BtnDemo");
            DataTable dt2 = new DataTable();
            dt2.Columns.Add(new DataColumn("MyProperty", typeof(int)));
            dt2.Columns.Add(new DataColumn("Image", typeof(byte[])));
            DataRow dr2 = dt2.NewRow();
            dr2["MyProperty"] = 111;
            dr2["Image"] = Generate1("qwertyu");

            dt2.Rows.Add(dr2);
            ReportDataSource DataSet2 = new ReportDataSource("DataSet1", dt2);
            this.reportViewer1.LocalReport.DataSources.Clear();
            this.reportViewer1.LocalReport.ReportPath = @"Report1.rdlc";//设置报表路径
            this.reportViewer1.LocalReport.DataSources.Add(DataSet2);
            this.reportViewer1.RefreshReport();//刷新报表
            this.reportViewer1.RefreshReport();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            RDLCPrinter.BillPrint.Run(this.reportViewer1.LocalReport);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Generate1("heooll23467890-=WERTYIL;'llll");
            Generate2("12345678");
        }
        /// <summary>
        /// 生成二维码,保存成图片
        /// </summary>
        public byte[] Generate1(string text)
        {
            BarcodeWriter writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            QrCodeEncodingOptions options = new QrCodeEncodingOptions();
            options.DisableECI = true;
            //设置内容编码
            options.CharacterSet = "UTF-8";
            //设置二维码的宽度和高度
            options.Width = 500;
            options.Height = 500;
            //设置二维码的边距,单位不是固定像素
            options.Margin = 1;
            writer.Options = options;

            Bitmap map = writer.Write(text);
            System.IO.MemoryStream MStream = new System.IO.MemoryStream();
            map.Save(MStream, System.Drawing.Imaging.ImageFormat.Png);
            return MStream.ToArray();


            this.pictureBox1.Image = map;

          //  string filename = @"H:\桌面\截图\generate1.png";
            //map.Save(filename, ImageFormat.Png);
            //map.Dispose();
        }
        /// <summary>
        /// 条形码
        /// </summary>
        /// <param name="text"></param>
         void Generate2(string text)
        {
            BarcodeWriter writer = new BarcodeWriter();
            //使用ITF 格式，不能被现在常用的支付宝、微信扫出来
            //如果想生成可识别的可以使用 CODE_128 格式
            //writer.Format = BarcodeFormat.ITF;
            writer.Format = BarcodeFormat.CODE_128;
            EncodingOptions options = new EncodingOptions()
            {
                Width = 150,
                Height = 50,
                Margin = 2
            };
            writer.Options = options;
            Bitmap map = writer.Write(text);
            this.pictureBox1.Image = map;
            //string filename = @"H:\桌面\截图\generate2.png";
           // map.Save(filename, ImageFormat.Png);
        }
    }
}
